package com.example.app_translation.util;

import com.alibaba.fastjson.JSONArray;

public class DealSQL {
    private  static  String BASE_SQL_INSERT="insert into ";
    private  static  String BASE_SQL_DROP="drop table if exists ";
    private  static  String BASE_SQL_CREATE="CREATE TABLE ";
    /*
     * @Description:
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param:api 表名，words 字段列，jsondata 值
     * @return: 一个插入sql的语句
     **/
    public String getInsertSql(String api,JSONArray words,String jsondata){
        JSONArray array = JSONArray.parseArray(jsondata);
        StringBuilder create_sql = new StringBuilder();
        create_sql.append(BASE_SQL_INSERT).append(" ");
        create_sql.append(api+" (\n");
        for (int i = 0; i <words.size() ; i++) {
            if(i==words.size()-1){
                create_sql.append(words.getJSONObject(i).getString("apiname")).append(" )");
            }else{
                create_sql.append(words.getJSONObject(i).getString("apiname")).append(",");
            }
        }
        create_sql.append(" values\n");
        for (int i = 0; i <array.size() ; i++) {
            create_sql.append("(");
            for (int j = 0; j <words.size() ; j++) {
                if(j==words.size()-1){
                    if(i==array.size()-1){
                        create_sql.append("'").append(array.getJSONObject(i).getString(words.getJSONObject(j).getString("apiname"))).append("'").append(")\n");
                    }else{
                        create_sql.append("'").append(array.getJSONObject(i).getString(words.getJSONObject(j).getString("apiname"))).append("'").append("),\n");
                    }
                }else{
                    create_sql.append("'").append(array.getJSONObject(i).getString(words.getJSONObject(j).getString("apiname"))).append("'").append(",");
                }
            }
            }
          return create_sql.toString();
        }
    /*
     * @Description:
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null: api对象
     * @return: 删除表
     **/
    public String getDropSql(String api){
        String drop_sql=BASE_SQL_DROP+api;
        return drop_sql;
    }
    /*
     * @Description:
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public String getCreateSql(String api,JSONArray words){
        StringBuilder create_sql = new StringBuilder();
        create_sql.append(BASE_SQL_CREATE).append(api).append(" (");
        for (int i = 0; i < words.size(); i++) {
            String label_name = words.getJSONObject(i).getString("label_name");
            create_sql.append(words.getJSONObject(i).getString("apiname"));
            if(i==words.size()-1){
                create_sql.append(" text COMMENT '").append(label_name).append("' ");
            }else{
                create_sql.append(" text COMMENT '").append(label_name).append("', ");
            }
        }
        create_sql.append(")");
        System.out.println("create_sql :::" + create_sql.toString());
        return create_sql.toString();
    }

}
